package com.jiang.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author Administrator
 *
 */

public class HRFilter implements Filter {

	private String encoding = "UTF-8";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("过滤器初始化!");
		String init_encoding = filterConfig.getInitParameter("encoding");
		if (StringUtils.isNotBlank(init_encoding)) {
			this.encoding = init_encoding;
		}
	}

	/**
	 * 在其中设置编码方式，省的每次请求都去设置
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		// 设置编码
		request.setCharacterEncoding(this.encoding);
		response.setCharacterEncoding(this.encoding);
		// 放行
		chain.doFilter(request, response);

	}

	@Override
	public void destroy() {

	}

}
